package com.snline.crm.logistics.action;

import java.util.List;

import javax.annotation.Resource;

import com.snline.crm.logistics.entity.ProjectInfo;
import com.snline.crm.logistics.entity.ProjectTrack;
import com.snline.crm.logistics.entity.ProjectUserInfo;
import com.snline.crm.logistics.service.TrackService;

public class TrackAction extends BaseAction{

    /**
     * 序列化
     */
    private static final long serialVersionUID = 1L;

    @Resource
    private TrackService trackService;
    
    // 画面上表示数据
    private List<ProjectTrack> trackIng;
    private List<ProjectTrack> trackOver;
    private List<ProjectTrack> trackDel;
    private ProjectTrack pjTrack;
    private List<ProjectInfo> projectInfos;
    private List<ProjectUserInfo> projectUserInfos;
    
    // 下拉刷新的数据标签
    private String flag;
    
    /**
     * 画面一览的明细处理
     * @return
     */
    public String getDetail() {
        
        String userId = session.get("userId").toString();
        
        this.trackIng = trackService.selectProjectTrackStats(userId, "1", 0);
        this.trackOver = trackService.selectProjectTrackStats(userId, "2", 0);
        this.trackDel = trackService.selectProjectTrackStats(userId, "3",0);
        
        return SUCCESS;
    }

    public String getTrackList() {

        String userId = session.get("userId").toString();
        
        if (this.flag == null || "".equals(this.flag)) {

            return ERROR;
        }
        if (this.flag.equals("1")) {

            this.trackIng = trackService.selectProjectTrackStats(userId, "1", this.page);
            
            return "trackIng";
        } else if (this.flag.equals("2")) {

            this.trackOver = trackService.selectProjectTrackStats(userId, "2", this.page);
            return "trackOver";
        } else if (this.flag.equals("3")) {
            
            this.trackDel = trackService.selectProjectTrackStats(userId, "3", this.page);
            return "trackDel";
        }
        return ERROR;
    }
    /**
     * 取得进度的数据
     * @return
     */
    public String getTrack() {
        
        String trackId = servletRequest.getParameter("trackId");
        this.pjTrack = trackService.selectTrack(trackId);
        
        this.projectInfos = trackService.selectProjectInfos();
        this.projectUserInfos = trackService.selectProjectUserInfos();
        
        return SUCCESS;
    }
    
    /**
     * 插入数据
     * @return
     */
    public String insert() {
        
        trackService.insert(pjTrack);
        
        return SUCCESS;
    }
    
    public String updateByAdmin() {
        
        if (this.pjTrack.getTrackPct().compareTo("100")>=0) {
            
            this.pjTrack.setTrackPct("2");
        }
        
        trackService.updateByAdmin(this.pjTrack);
        
        return SUCCESS;
    }
    
    public String saveTrack() {
        
        if (this.pjTrack == null) {
            return ERROR;
        }
        
        if (pjTrack.getTrackId() == null || "".equals(pjTrack.getTrackId())) {

            if (this.pjTrack.getTrackPct().compareTo("100")>=0) {
                
                this.pjTrack.setStatsFlg("2");
            }else {

                this.pjTrack.setStatsFlg("1");
                
            }
            trackService.insert(this.pjTrack);
        } else {

            if (this.pjTrack.getTrackPct().compareTo("100")>=0) {
                
                this.pjTrack.setStatsFlg("2");
            } else {

                this.pjTrack.setStatsFlg("1");
                
            }
            trackService.updateByAdmin(this.pjTrack);
        }
        
        
        return SUCCESS;
    }
    
    public String delTrack() {
        
        String trackId = servletRequest.getParameter("trackId");
        trackService.delTrack(trackId);
        return SUCCESS;
    }
    
    public String createTrack() {
    
        this.projectInfos = trackService.selectProjectInfos();
        this.projectUserInfos = trackService.selectProjectUserInfos();
        return SUCCESS;
    }
    
    /**
     * @return the trackIng
     */
    public List<ProjectTrack> getTrackIng() {
        return trackIng;
    }

    /**
     * @param trackIng the trackIng to set
     */
    public void setTrackIng(List<ProjectTrack> trackIng) {
        this.trackIng = trackIng;
    }

    /**
     * @return the trackOver
     */
    public List<ProjectTrack> getTrackOver() {
        return trackOver;
    }

    /**
     * @param trackOver the trackOver to set
     */
    public void setTrackOver(List<ProjectTrack> trackOver) {
        this.trackOver = trackOver;
    }

    /**
     * @return the trackDel
     */
    public List<ProjectTrack> getTrackDel() {
        return trackDel;
    }

    /**
     * @param trackDel the trackDel to set
     */
    public void setTrackDel(List<ProjectTrack> trackDel) {
        this.trackDel = trackDel;
    }

    /**
     * @return the pjTrack
     */
    public ProjectTrack getPjTrack() {
        return pjTrack;
    }

    /**
     * @param pjTrack the pjTrack to set
     */
    public void setPjTrack(ProjectTrack pjTrack) {
        this.pjTrack = pjTrack;
    }

    /**
     * @return the flag
     */
    public String getFlag() {
        return flag;
    }

    /**
     * @param flag the flag to set
     */
    public void setFlag(String flag) {
        this.flag = flag;
    }

    /**
     * @return the projectInfos
     */
    public List<ProjectInfo> getProjectInfos() {
        return projectInfos;
    }

    /**
     * @param projectInfos the projectInfos to set
     */
    public void setProjectInfos(List<ProjectInfo> projectInfos) {
        this.projectInfos = projectInfos;
    }

    /**
     * @return the projectUserInfos
     */
    public List<ProjectUserInfo> getProjectUserInfos() {
        return projectUserInfos;
    }

    /**
     * @param projectUserInfos the projectUserInfos to set
     */
    public void setProjectUserInfos(List<ProjectUserInfo> projectUserInfos) {
        this.projectUserInfos = projectUserInfos;
    }
    
    
}
